'use strict';

const Controller = require('egg').Controller;
const fs = require('fs');
const path = require('path');
const Pump = require('mz-modules/pump');
class DownloadfileController extends Controller {
    async download() {
        try {
            // 文件路径，egg的文件路径默认是从项目的文件夹开始的。
            // 如果想动态的获取不同的文件，可以根据传递的参数在这里拼出路径
            console.log('aaaaaaaaaaaaaaaaa');
            const filePath = path.resolve('app/public/openTopic', 'test.xlsx');
            // this.ctx.attachment('hello.txt');test.xlsx

            // // 设置请求头
            // this.ctx.set('Content-Type', 'text/plain');
            // // 把数据流赋予body
            // this.ctx.body = fs.readFileSync(filePath);
            this.ctx.attachment('test.xlsx');
            this.ctx.set('Content-Type', 'application/octet-stream');
            // this.ctx.body = fs.createReadStream(filePath);

            const test = fs.createReadStream(filePath);
            console.log('test::::::::::::::::',test.pipe(this.ctx.body));
            this.ctx.body = test.pipe(this.ctx.body);
        } catch (error) {
            console.log('Error:',error);
            return { state: 0 }
        }
    }
}
module.exports = DownloadfileController;
